home *** CD-ROM | disk | FTP | other *** search
/ Programming in Microsoft Windows with C# / Programacion en Microsoft Windows con C#.iso / Original Code / Pages and Transforms / TenCentimeterRuler / TenCentimeterRuler.cs next >
Encoding:
Text File  |  2001-01-15  |  2.5 KB  |  70 lines

  1. //-------------------------------------------------
  2. // TenCentimeterRuler.cs ⌐ 2001 by Charles Petzold
  3. //-------------------------------------------------
  4. using System;
  5. using System.Drawing;
  6. using System.Windows.Forms;
  7.  
  8. class TenCentimeterRuler: PrintableForm
  9. {
  10.      public new static void Main()
  11.      {
  12.           Application.Run(new TenCentimeterRuler());
  13.      }
  14.      public TenCentimeterRuler()
  15.      {
  16.           Text = "Ten-Centimeter Ruler";
  17.      }
  18.      protected override void DoPage(Graphics grfx, Color clr, int cx, int cy)
  19.      {
  20.           Pen       pen   = new Pen(clr);
  21.           Brush     brush = new SolidBrush(clr);
  22.           const int xOffset = 10;
  23.           const int yOffset = 10;
  24.  
  25.           grfx.DrawPolygon(pen, new PointF[] 
  26.                { 
  27.                     MMConv(grfx, new PointF(xOffset,       yOffset)),
  28.                     MMConv(grfx, new PointF(xOffset + 100, yOffset)),
  29.                     MMConv(grfx, new PointF(xOffset + 100, yOffset + 10)),
  30.                     MMConv(grfx, new PointF(xOffset,       yOffset + 10))
  31.                });
  32.  
  33.           StringFormat strfmt = new StringFormat();
  34.           strfmt.Alignment = StringAlignment.Center;
  35.  
  36.           for (int i = 1; i < 100; i++)
  37.           {
  38.                if (i % 10 == 0)         // Centimeter markings
  39.                {
  40.                     grfx.DrawLine(pen, 
  41.                          MMConv(grfx, new PointF(xOffset + i, yOffset)),
  42.                          MMConv(grfx, new PointF(xOffset + i, yOffset + 5)));
  43.  
  44.                     grfx.DrawString((i/10).ToString(), Font, brush, 
  45.                          MMConv(grfx, new PointF(xOffset + i, yOffset + 5)), 
  46.                          strfmt);
  47.                }
  48.                else if (i % 5 == 0)     // Half-centimeter markings
  49.                {
  50.                     grfx.DrawLine(pen, 
  51.                          MMConv(grfx, new PointF(xOffset + i, yOffset)),
  52.                          MMConv(grfx, new PointF(xOffset + i, yOffset + 3)));
  53.                }
  54.                else                     // Millimeter markings
  55.                {
  56.                     grfx.DrawLine(pen, 
  57.                       MMConv(grfx, new PointF(xOffset + i, yOffset)),
  58.                       MMConv(grfx, new PointF(xOffset + i, yOffset + 2.5f)));
  59.                }
  60.           }
  61.      }
  62.      PointF MMConv(Graphics grfx, PointF pointf)
  63.      {
  64.          pointf.X *= grfx.DpiX / 25.4f;
  65.          pointf.Y *= grfx.DpiY / 25.4f;
  66.  
  67.          return pointf;
  68.      }
  69. }
  70.